package cn.xz.mytimer

import android.content.Context
import android.content.SharedPreferences

/**
 * 本地存储
 * */
object SessionUtils {

    var JC_SP_KEY = "JC_SP_KEY"

    //是否显示间隔动画
    var SHOW_ANI = "SHOW_ANI"

    //间隔动画秒数
    var SHOW_ANI_TIME = "SHOW_ANI_TIME"

    //是否显示 年月日
    var SHOW_YEAR = "SHOW_YEAR"

    //是否显示秒数
    var SHOW_S = "SHOW_S"

    //是否显示星期几
    var SHOW_WEEK = "SHOW_WEEK"

    //是否开启闹钟
    var SHOW_NZ = "SHOW_NZ"

    //是否重复闹钟
    var NZ_CF = "NZ_CF"

    //设置闹铃时间
    var MY_TIMER = "MY_TIMER"

    //闹钟内容
    var MY_NZ_CONTENT = "MY_NZ_CONTENT"

    //是否开启智能语音功能
    var ZN_AI = "ZN_AI"

    private var preferences: SharedPreferences? = null

    private val sharePreferences: SharedPreferences
        get() {
            if (preferences == null) {
                synchronized(SharedPreferences::class.java) {
                    if (preferences == null) {
                        preferences = BaseApplication.getContext().getSharedPreferences(
                            JC_SP_KEY,
                            Context.MODE_PRIVATE
                        )
                    }
                }
            }
            return preferences!!
        }

    private val editor: SharedPreferences.Editor
        get() = sharePreferences.edit()

    fun clearAll(){
        editor.clear()
        editor.commit()
    }

    fun getString(key: String, value: String): String {
        var s = value
        val m = sharePreferences.getString(key, value)
        if (m != null && m.isNotEmpty()){
            s = m
        }
        return s
    }

    fun getBoolean(key: String, value: Boolean): Boolean {
        return sharePreferences.getBoolean(key, value)
    }

    fun getInt(key: String, value: Int): Int {
        return sharePreferences.getInt(key, value)
    }

    fun getLong(key: String, value: Long): Long {
        return sharePreferences.getLong(key, value)
    }

    fun getFloat(key: String, value: Float): Float {
        return sharePreferences.getFloat(key, value)
    }

    fun getStringSet(key: String, value: Set<String>): Set<String>? {
        return sharePreferences.getStringSet(key, value)
    }

    fun commitString(key: String, value: String) {
        val editor = editor.putString(key, value)
        commit(editor)
    }

    fun commitInt(key: String, value: Int) {
        val editor = editor.putInt(key, value)
        commit(editor)
    }

    fun commitFloat(key: String, value: Float) {
        val editor = editor.putFloat(key, value)
        commit(editor)
    }

    fun commitLong(key: String, value: Long) {
        val editor = editor.putLong(key, value)
        commit(editor)
    }

    fun commitBoolean(key: String, value: Boolean) {
        val editor = editor.putBoolean(key, value)
        commit(editor)
    }

    fun commitStringSet(key: String, value: Set<String>) {
        val editor = editor.putStringSet(key, value)
        commit(editor)
    }

    fun remove(key: String) {
        editor.remove(key).commit()
    }

    private fun commit(editor: SharedPreferences.Editor) {
        editor.commit()
    }
}